package com.platform.common.secret.utils.sm4;


import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import java.security.*;
import java.util.Arrays;


/**
 * @Author: liangyinan3
 * @Date: 2018/10/9 16:41
 * @Describe: SM4算法(更高的安全等级)
 */
public class SM4SeniorUtil {
    static {
        Security.addProvider(new BouncyCastleProvider());
    }
    public static final String ENCODING = "UTF-8";
    public static final String ALGORITHM_NAME = "SM4";
    //加密算法/分组加密模式/分组填充方式
    //PKCS5Padding-以8个字节为一组进行分组加密
    //定义分组加密模式使用:PKCS5Padding
    public static final String ALGORTHM_NAME_ECB_PADDING = "SM4/ECB/PKCS5Padding";

    public static final int DEFAULT_KEY_SIZE = 128;
    /**
     * 生成ECB暗号
     * @explain ECB模式(电子密码本模式)
     * @param algorithmName
     *        算法名称
     * @param mode
     *        模式
     * @param key
     * @return
     * @throw Exception
     */
    private static Cipher generateEcbCipher(String algorithmName, int mode, byte[] key) throws Exception {
        Cipher cipher= Cipher.getInstance(algorithmName,BouncyCastleProvider.PROVIDER_NAME);
        Key sm4Key=new SecretKeySpec(key,algorithmName);
        cipher.init(mode,sm4Key);
        return cipher;
    }
    /**
     * 自动生成秘钥
     * @explain
     * @return
     * @throw Exception
     */
    public static byte[] generateKey() throws NoSuchAlgorithmException, NoSuchProviderException {
        return generateKey(DEFAULT_KEY_SIZE);
    }

    public static byte[] generateKey(int keySize) throws NoSuchAlgorithmException, NoSuchProviderException {
        KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM_NAME, BouncyCastleProvider.PROVIDER_NAME);
        kg.init(keySize, new SecureRandom());
        return kg.generateKey().getEncoded();
    }
     /**
     * SM4加密
     * @explain ECB加密模式
     * @param hexKey
     *        16进制秘钥(忽略大小写)
     * @param paramStr
     *        明文
     * @return 16进制加密字符串
     * @throw Exception
     */
    public static String encryptEcb(String hexKey, String paramStr) throws Exception {
        String cipherText="";
        byte[] keyData= ByteUtils.fromHexString(hexKey);
        byte[] scrData=paramStr.getBytes(ENCODING);
        byte[] cipherArray=encryptEcbPadding(keyData,scrData);
        cipherText= ByteUtils.toHexString(cipherArray);
        return cipherText;
    }
    /**
     * 加密模式ECB
     * @explain ECB加密模式
     * @param key
     * @param data
     * @return
     * @throw Exception
     */
    private static byte[] encryptEcbPadding(byte[] key,byte[] data) throws Exception {
        Cipher cipher = generateEcbCipher(ALGORTHM_NAME_ECB_PADDING, Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(data);
    }
    /**
     * SM4解密
     * @explain ECB解密模式
     * @param hexKey
     *        16进制秘钥(忽略大小写)
     * @param cipherText
     *        密文
     * @return 16进制解密字符串
     * @throw Exception
     */
    public static String decryptEcb(String hexKey, String cipherText) throws Exception {
        String decryptStr="";
        byte[] keyData= ByteUtils.fromHexString(hexKey);
        byte[] cipherData=ByteUtils.fromHexString(cipherText);
        byte[] srcData=decryptEcbPadding(keyData,cipherData);
        decryptStr= new String(srcData,ENCODING);
        return decryptStr;
    }
    /**
     * 解密模式ECB
     * @explain
     * @param key
     * @param data
     * @return
     * @throw Exception
     */
    private static byte[] decryptEcbPadding(byte[] key,byte[] data) throws Exception {
        Cipher cipher = generateEcbCipher(ALGORTHM_NAME_ECB_PADDING, Cipher.DECRYPT_MODE, key);
        return cipher.doFinal(data);
    }
    /**
     * 加密数据校验
     * @explain ECB解密模式
     * @param hexKey
     *        16进制秘钥(忽略大小写)
     * @param paramStr
     *        明文
     * @param cipherText
     *        密文
     * @return true 数据相同 flase 不同
     * @throw Exception
     */
    public static boolean equalEcb(String hexKey, String paramStr, String cipherText) throws Exception {
        boolean flag=false;
        byte[] keyData= ByteUtils.fromHexString(hexKey);
        byte[] cipherData=ByteUtils.fromHexString(cipherText);
        byte[] decryptData=decryptEcbPadding(keyData,cipherData);
        byte[] srcData=paramStr.getBytes(ENCODING);
        flag= Arrays.equals(srcData,decryptData);
        return flag;
    }
}

